package com.sdhs.listmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdhs.common.core.domain.BaseEntity;
import com.sdhs.common.core.domain.event.ProcessEvent;
import com.sdhs.common.core.page.PageQuery;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessStatusEnum;
import com.sdhs.common.utils.ConvertUtils;
import com.sdhs.costcontrol.domain.GcControlChangeManagement;
import com.sdhs.file.utils.ManageListUtil;
import com.sdhs.listmanage.domain.bo.GcProjectManageListBo;
import com.sdhs.listmanage.domain.entity.GcManageList;
import com.sdhs.listmanage.domain.entity.GcProjectManageList;
import com.sdhs.listmanage.domain.vo.GcProjectManageListVo;
import com.sdhs.listmanage.mapper.GcManageListMapper;
import com.sdhs.listmanage.mapper.GcProjectManageListMapper;
import com.sdhs.listmanage.service.IGcManageListService;
import com.sdhs.listmanage.service.IGcProjectManageListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 项目清单Service业务层处理
 *
 * @author may
 * @date 2023-07-21
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class GcProjectManageListServiceImpl implements IGcProjectManageListService {

    private final GcProjectManageListMapper baseMapper;

    @Autowired
    private GcManageListMapper gcManageListMapper;
    @Autowired
    private IGcManageListService gcManageListService;

    /**
     * 查询项目清单
     */
    @Override
    public GcProjectManageListVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 通过项目ID查询项目清单
     */
    @Override
    public GcProjectManageListVo getInfoByProjectId(Long projectId) {
        return baseMapper.selectVoByProjectId(projectId);
    }

    /**
     * 总体流程监听(例如: 提交 退回 撤销 终止 作废等)
     * 正常使用只需#processEvent.key=='leave1'
     * 示例为了方便则使用startsWith匹配了全部示例key
     *
     * @param processEvent 参数
     */
    @org.springframework.context.event.EventListener(condition = "#processEvent.key.startsWith('manage_list')")
    public void processHandler(ProcessEvent processEvent) {

        // 已完成（已通过）
        if (BusinessStatusEnum.FINISH.getStatus().equals(processEvent.getStatus())) {
            String businessKey = processEvent.getBusinessKey();
            GcProjectManageListVo gcProjectManageListVo = baseMapper.selectVoByInstanceId(businessKey);
             List<GcManageList> gcManageLists = gcManageListMapper.selectGcManageListByProject(gcProjectManageListVo.getProjectId());
            /*审批结束之后计算清单--标前利润率*/
             ManageListUtil.UpdateMeasurement(gcProjectManageListVo.getProjectId(),gcManageLists);
             /*清单锁定*/
            gcManageListService.update(gcManageLists.get(0));
        }

    }
    /**
     * 查询项目清单列表
     */
    @Override
    public TableDataInfo queryPageList(GcProjectManageListBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GcProjectManageList> lqw = buildQueryWrapper(bo);
        Page<GcProjectManageListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    /**
     * 查询项目清单列表
     */
    @Override
    public List<GcProjectManageListVo> queryList(GcProjectManageListBo bo) {
        LambdaQueryWrapper<GcProjectManageList> lqw = buildQueryWrapper(bo);
        List<GcProjectManageListVo> list = baseMapper.selectVoList(lqw);

        return list;
    }

    private LambdaQueryWrapper<GcProjectManageList> buildQueryWrapper(GcProjectManageListBo bo) {
        LambdaQueryWrapper<GcProjectManageList> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增项目清单
     */
    @Override
    public GcProjectManageListVo insertByBo(GcProjectManageListBo bo) {
        GcProjectManageList add = ConvertUtils.sourceToTarget(bo, GcProjectManageList.class);
        add.setId(IdWorker.getId());
        add.setApprovalStatus(BusinessStatusEnum.DRAFT.getStatusValue());
        add.setIsDelete("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return ConvertUtils.sourceToTarget(add, GcProjectManageListVo.class);
    }

    /**
     * 修改项目清单
     */
    @Override
    public GcProjectManageListVo updateByBo(GcProjectManageListBo bo) {
        GcProjectManageList update = ConvertUtils.sourceToTarget(bo, GcProjectManageList.class);
        baseMapper.updateById(update);
        return ConvertUtils.sourceToTarget(update, GcProjectManageListVo.class);
    }

    /**
     * 批量删除项目清单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return baseMapper.deleteByIds(ids) > 0;
    }
}
